﻿using OCR.Config;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OCR.SystemThread
{
    class NGControlThread
    {
        public static bool NGControlThread_Flowrec = false;
        public static Thread NGControlFlow_Thread = null;

        public static int iNGControl_Step = 0;

        public static bool NGControlThread_Pause = false;

        public static DateTime startDt;

        public static ConcurrentQueue<FinalInfoData> infoQueue = new ConcurrentQueue<FinalInfoData>();

        public static void NGControl_Thread()
        {
            iNGControl_Step = 0;
            NGControlThread_Pause = false;
            NGControlThread_Flowrec = true;
            NGControlFlow_Thread = new Thread(NGControlThreadFlow);
            NGControlFlow_Thread.IsBackground = true;
            NGControlFlow_Thread.Start();
            Console.WriteLine("----NGControl_Thread启动");
        }

        private static void NGControlThreadFlow()
        {
            while (NGControlThread_Flowrec)
            {
                if (!NGControlThread_Pause)
                {
                    switch (iNGControl_Step)
                    {
                        case 0:
                            if (IOThread_Run.Di[2] == 1)
                            {
                                infoQueue.TryDequeue(out FinalInfoData finalInfoData);
                                if (finalInfoData != null)
                                {
                                    Console.WriteLine("-----NG检测一次----"+ finalInfoData.result);
                                    if (finalInfoData.result == "NG")
                                    {
                                        Console.WriteLine("-----输出结果一次----");

                                        IOThread_Run.DO_Trigger_olng(1, 1);
                                        Console.WriteLine("-----发送打开DO一次----");
                                        Thread.Sleep(int.Parse(LocalInfo.NG_Action));
                                        Console.WriteLine("-----吹气一次----");
                                        IOThread_Run.DO_Trigger_olng(1, 0);
                                        Console.WriteLine("-----发送关闭DO一次----");
                                    }
                                    else if (finalInfoData.result == "OK")
                                    {
                                        break;
                                    }
                                }

                                //Console.WriteLine("检测队列:------------------------"+ infoQueue.Count);
                                foreach (var item in infoQueue)
                                {
                                    //Console.WriteLine("检测元素:------------------------"+ item.result);
                                }

                            }

                            break;
                        case 5:
                           
                            break;
                        case 10:

                            break;

                        case 15:

                            break;
                        case 20:

                            break;
                        case 25:

                            break;
                    }
                }


            }

        }
    }
}
